using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TowerDefenseXNA.Cenas;

namespace TowerDefenseXNA
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        KeyboardState teclado_atual, teclado_anterior;
        MouseState mouse_atual, mouse_anterior;
        GamePadState joystick_atual, joystick_anterior;

        Intro intro;
        Fase_1 fase_1;

        SpriteFont Arial36;

        int pontos = 100;

        public enum Estados { INTRO, MENU, CREDITOS, CONFIGURACAO, SAIR, GAME_OVER, THE_END, JOGO, PAUSE, };

        enum Fases
        {
            FASE_1,
            FASE_2,
            FASE_3,
        };

        public static Estados estado_atual = Estados.INTRO;

        Fases fase_atual = Fases.FASE_1;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            IsMouseVisible = true;
            Window.Title = "Tower Defense XNA";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            //graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            intro = new Intro(Content, Window);
            fase_1 = new Fase_1(Content, Window);

            Arial36 = Content.Load<SpriteFont>("Arial36");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Window.Title = mouse_atual.X.ToString() + "-" + mouse_atual.Y.ToString();

            // TODO: Add your update logic here
            teclado_atual = Keyboard.GetState();
            mouse_atual = Mouse.GetState();
            joystick_atual = GamePad.GetState(PlayerIndex.One);

            switch (estado_atual)
            {
                case Estados.INTRO:
                    {
                        intro.Update(gameTime, teclado_atual, 
teclado_anterior, mouse_atual, mouse_anterior);
                    }
                    break;

                case Estados.JOGO:
                    {
                        fase_1.Update(gameTime, teclado_atual,teclado_anterior,mouse_atual,mouse_anterior);
                    }//case JOGO
                    break;
            }//fim SWITCH

            
            if (teclado_atual.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            if (teclado_atual.IsKeyDown(Keys.F11))
            {
                graphics.ToggleFullScreen();
            }
            
            teclado_anterior = teclado_atual;
            mouse_anterior = mouse_atual;
            joystick_anterior = joystick_atual;



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            switch (estado_atual)
            {
                case Estados.INTRO:

                    break;

                case Estados.JOGO:

                    switch (fase_atual)
                    {
                        case Fases.FASE_1:
                            fase_1.Draw(gameTime, spriteBatch);
                            spriteBatch.DrawString(Arial36, "pontos = " + pontos, Vector2.Zero, Color.Red);
                            break;
                        case Fases.FASE_2:
                            break;

                        default:

                            break;
                    }

                    break;


            }
           
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
